import Vue from 'vue';
import VueRouter from 'vue-router';
/* import home from '@/pages/home'; */
import login from '@/pages/login';
import register from '@/pages/register';
/* import search from '@/pages/search'; */
import Detail from '@/pages/Detail';
import AddCartSuccess from '@/pages/AddCartSuccess';
import ShopCart from '@/pages/ShopCart';
import store from '@/store';
import trade from '@/pages/Trade';
import pay from '@/pages/Pay'
import paySuccess from '@/pages/PaySuccess'
import center from '@/pages/Center'

//个人中心二级路由组件
import myOrder from '@/pages/Center/myOrder'
import groupOrder from '@/pages/Center/groupOrder'

Vue.use(VueRouter);

const routes = [
  {
    path: '/center',
    name: 'center',
    component: center,
    meta: { show: false },
    //个人中心二级路由
    children: [
      {
        path: 'myOrder',
        component: myOrder
      }, {
        path: 'groupOrder',
        component: groupOrder
      }, {
        path: '/center',
        redirect: '/center/myOrder'
      }
    ]
  },
  {
    path: '/paySuccess',
    name: 'paySuccess',
    component: paySuccess,
    meta: { show: false },
  },
  {
    path: '/pay',
    name: 'pay',
    component: pay,
    meta: { show: false },
    //路由独享守卫，规定支付页只能由交易页进行跳转
    beforeEnter: (to, from, next) => {
      if (from.path == '/trade') {
        next();
      } else {
        next(false);
      }
    }
  },
  {
    path: '/trade',
    name: 'trade',
    component: trade,
    meta: { show: false },
    //路由独享守卫，规定交易页只能由购物车进行跳转
    beforeEnter: (to, from, next) => {
      if (from.path == '/shopCart') {
        next();
      } else {
        next(false);
      }
    }
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/pages/home'),
    meta: { show: true },
  },
  {
    path: '/login',
    name: 'login',
    component: login,
    meta: { show: false },
  },
  {
    path: '/register',
    name: 'register',
    component: register,
    meta: { show: false },
  },
  {
    /* path:'/search/:keyword', */
    path: '/search/:keyword?',
    name: 'search',
    component: () => import('@/pages/search'),
    meta: { show: true },
  },
  {
    path: '/Detail/:skuid',
    name: 'Detail',
    component: Detail,
    meta: { show: true },
  },
  {
    path: '/AddCartSuccess',
    name: 'AddCartSuccess',
    component: AddCartSuccess,
    meta: { show: true },
  },
  {
    path: '/shopCart',
    name: 'shopCart',
    component: ShopCart,
    meta: { show: true },
  },
  //重定向到首页
  {
    path: '/',
    redirect: '/home'
  },
]

const router = new VueRouter({
  routes,
  // eslint-disable-next-line
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { y: 0 }
  },
});
/* eslint-disable */
router.beforeEach(async (to, from, next) => {
  let token = store.state.user.token;
  let name = store.state.user.userInfo.name;
  //通过是否有token分已登录和未登录两种情况
  if (token) {
    //已登录，不允许再次跳转登录页以及注册页
    if (to.path == '/login' || to.path == '/register') {
      next('/home');
    } else {
      //已登录，跳转非登录类组件时，需要保证每次都有用户信息userInfo，如果没有则发请求重新获取；
      if (name) {
        next();
      } else {
        try {
          await store.dispatch("getUserInfo");
          next();
        } catch (error) {
          //token要是过期了，就会导致请求获取用户信息失败，这时需要退出登录然后重新让用户登录
          store.dispatch('logout');
          router.push('/login');
        }
      }
    }
  } else {
    let topath = to.path;
    //未登录状态下不能去交易、支付相关、个人中心相关的页面
    if (topath.indexOf('/trade') != -1 || topath.indexOf('/pay') != -1 || topath.indexOf('/center') != -1) {
      next(`/login?redirect=${topath}`);
    } else {
      //去的不是上面这些，就放行，比如home、search、shopCart等直接放行
      next();
    }
  }
});
/* eslint-disable */

export default router
